﻿using System.Collections;
using UnityEngine;

namespace ProjectDanmaku.UI
{
    public class ProgressBar : MonoBehaviour
    {
        [SerializeField] private float maxWidth;
        [SerializeField] private RectTransform progressPartRect;

        public void SetProgress(float percent)
        {
            var size = progressPartRect.sizeDelta;
            size.x = percent * maxWidth;
            progressPartRect.sizeDelta = size;
        }

        private float AnimCurve(float t)
        {
            return Mathf.Pow(t, 0.5f);
        }

        public IEnumerator SetProgressAnim(float percent)
        {
            var startVal = progressPartRect.rect.width;
            var targetVal = percent * maxWidth;

            float t = 0;

            while (t > 1)
            {
                Mathf.Lerp(startVal, targetVal, AnimCurve(t));

                t += Time.deltaTime;

                yield return null;
            }
        }
    }
}